#######################################
#Open Ended Category Use: Figure 6
#######################################

#######################
#Packages
#######################
##Load libraries
library(corrr)
library(cowplot)
#data manipulation and plotting
library(broom)
library(rio)
library(tidyverse)
library(ggstance)
library(ggpubr)


#######################
#Data
#######################

lucid <- import("./Data/lucid_coded_all.rda")


#######################
#Separate Programmatic Characteristics for top half of figure
#######################


###Getting summary statistics
dems <- lucid %>%
  filter(dem_extreme_1 == 1) %>%
  summarise(ideol = mean(d_ideol), 
            principles = mean(d_principles), 
            policy = mean(d_policy), 
            groups = mean(d_sgroups), 
            politicians = mean(d_politicians), 
            traits = mean(d_traits), 
            process = mean(d_process)) %>%
  mutate(party = "Democratic Party")

reps <- lucid %>%
  filter(rep_extreme_1 == 1) %>%
  summarise(ideol = mean(r_ideol), 
            principles = mean(r_principles), 
            policy = mean(r_policy), 
            groups = mean(r_sgroups), 
            politicians = mean(r_politicians), 
            traits = mean(r_traits), 
            process = mean(r_process)) %>%
  mutate(party = "Republican Party")

comb_means <- rbind(dems, reps)

comb_long <- comb_means %>%
  pivot_longer(cols=ideol:process, 
               names_to="category", 
               values_to="proportion") %>%
  mutate(category=factor(category, 
                         levels=c("ideol", "principles", "policy", 
                                  "groups", "politicians", "traits", "process"), 
                         labels=c("Ideology", "Principle", "Policy", 
                                  "Groups", "Politicians", "Traits", "Process")))

#######################
#Combined Programmatic Characteristics for bottom half
#######################

dems1 <- lucid %>%
  filter(dem_extreme_1 == 1) %>%
  mutate(programmatic = ifelse(d_policy ==1, 1, ifelse(d_ideol == 1, 1, 
                                                       ifelse(d_principles, 1, 0)))) %>%
  summarise(programmatic = mean(programmatic),  
            groups = mean(d_sgroups), 
            politicians = mean(d_politicians), 
            traits = mean(d_traits), 
            process = mean(d_process)) %>%
  mutate(party = "Democratic Party")


reps1 <- lucid %>%
  filter(rep_extreme_1 == 1) %>%
  mutate(programmatic = ifelse(r_policy ==1, 1, ifelse(r_ideol == 1, 1, 
                                                       ifelse(r_principles, 1, 0)))) %>%
  summarise(programmatic = mean(programmatic),  
            groups = mean(r_sgroups), 
            politicians = mean(r_politicians), 
            traits = mean(r_traits), 
            process = mean(r_process)) %>%
  mutate(party = "Republican Party")


comb_means1 <- rbind(dems1, reps1)

comb_long1 <- comb_means1 %>%
  pivot_longer(cols=programmatic:process, 
               names_to="category", 
               values_to="proportion") %>%
  mutate(category=factor(category, 
                         levels=c("programmatic", 
                                  "groups", "politicians", "traits", "process"), 
                         labels=c("Programmatic", 
                                  "Groups", "Politicians", "Traits", "Process")))
##########################
#Creating the figure
##########################

cdem <- comb_long %>%
  filter(party == "Democratic Party") 

crep <- comb_long %>%
  filter(party == "Republican Party") 

cdem1 <- comb_long1 %>%
  filter(party == "Democratic Party")

crep1 <- comb_long1 %>%
  filter(party == "Republican Party") 

d1 <- cdem %>%
  ggdotchart(x = "category", y="proportion", sorting="descending", 
             add = "segments", 
             dot.size=12, label=round(cdem$proportion , 2), 
             font.label = list(color = "black", size=10, vjust=0.5), 
             color="gray",
             rotate = T,
             ggtheme=theme_light(14)) + 
  labs(title="Target: Democratic Party", x = "Category", y = "Proportion") + 
  scale_y_continuous(limits=c(0,0.35))

r1 <- crep %>%
  ggdotchart(x = "category", y="proportion", sorting="descending", 
             add = "segments", 
             dot.size=12, label=round(crep$proportion , 2), 
             font.label = list(color = "black", size=10, vjust=0.5), 
             color="gray",
             rotate = T,
             ggtheme=theme_light(14)) + 
  labs(title="Target: Republican Party", x = "Category", y = "Proportion") +
  scale_y_continuous(limits=c(0,0.35))

d2 <- cdem1 %>%
  ggdotchart(x = "category", y="proportion", sorting="descending", 
             add = "segments", 
             dot.size=12, label=round(cdem1$proportion , 2), 
             font.label = list(color = "black", size=10, vjust=0.5), 
             color="gray", 
             rotate = T,
             ggtheme=theme_light(14)) + 
  labs(title="Target: Democratic Party", x = "Category", y = "Proportion") + 
  scale_y_continuous(limits=c(0,0.35))

r2 <- crep1 %>%
  ggdotchart(x = "category", y="proportion", sorting="descending", 
             add = "segments", 
             dot.size=12, label=round(crep1$proportion , 2), 
             font.label = list(color = "black", size=10, vjust=0.5), 
             color="gray", 
             rotate = T,
             ggtheme=theme_light(14)) + 
  labs(title="Target: Republican Party", x = "Category", y = "Proportion") +
  scale_y_continuous(limits=c(0,0.35))



comb_plot_final <- ggarrange(d1, r1, d2, r2, align="v", 
                             labels=c("A", "B", "C", "D"))

ggsave("Figure6.png", 
       plot=comb_plot_final, 
       height=7, width=14, dpi=1200)

